const app = getApp()

var innerAudioContext, playurl = [];
import { detailTab } from "../common/static/api_data.js"
import { throttle } from "../utils/throttle.js"
import { HiddenCall } from '../request/api.js'
const request = new HiddenCall
module.exports = {
  /**
 * 房客源详情
 * 房客源详情录音播放
 * 房客源底部描述，跟进，通话，详细调用
 * - @author          王磊
 * - @date             2018-11-10
 * detailTab           tab假数据
 * activeIndex        调用数据方法
 */
  data: {
    activeIndex: "queryDescriptionInfoServiceImpl",
    page: 1,
  },
  onLoad: function (options) {
    if (Object.keys(options).length !== 0) {
      let model = JSON.parse(options.model)
      // 房客源编号
      this.setData({
        code: model.resourceCode,
        resourceType: model.resourceType ? model.resourceType:''
      })
      // 是否限制拨打电话
      // if (model.isLimiteCall) {
      //   this.setData({ isLimiteCall: model.isLimiteCall })
      // }
      // 调用房源详情
      if (model.tactics === "houseServiceImpl") {
        this.houseDetail(model)
      } else {
        // 调用客源详情
        this.customerDetail(model)
      }
    }
    // 房客源详情描述跟进通话详细字典
    this.setData({ detailTab: detailTab })
    // 调用描述接口
    this.detailText(this.data.activeIndex)
    // 播放通话录音
    innerAudioContext = wx.createInnerAudioContext()
    // 播放通话结束
    innerAudioContext.onPlay(() => {})
    innerAudioContext.onEnded(() => {
      // 停止播放
      this.stopTelRecord()
    })
  },
  onUnload() {
    // 停止播放
    this.stopTelRecord()
  },
  /**
   * 播放房客源详情通话录音
   * author wl
   */
  playTelRecord(e) {
    // 播放地址
    let url = e.currentTarget.dataset.url
    // 选中的播放索引
    this.index = e.currentTarget.dataset.index
    // 开始播放
    innerAudioContext.src = url
    innerAudioContext.play()
    // 将当前的索引与地址赋予playurl数组
    playurl[this.index] = url
    // 将playurl数组放入模板model传到模板内
    this.data.infoContent.playurl = playurl
    // 将当前播放的索引传入模板model
    this.data.infoContent.playIndex = this.index
    // 数据更新
    this.setData({ infoContent: this.data.infoContent ? this.data.infoContent : null })
  },
  /**
 * 停止播放房客源详情通话录音
 * author wl
 */
  stopTelRecord(e) {
    // 判断当前是否有索引
    let index = e !== undefined ? e.currentTarget.dataset.index : this.index
    // 将当前索引的播放地址赋值为undefned

    index !== undefined ? this.data.infoContent.playurl[index] = 'undefined':''
    // 停止播放
    innerAudioContext.stop()
    // 数据更新
    this.setData({ infoContent: this.data.infoContent ? this.data.infoContent : null })
  }, 
   /**
   * 切换房客源详情tab页
   * author dzl
   */
  tabchange: function (e) {
    // 拿到切换调用的方法
    this.setData({ activeIndex: e.detail, page: 1 })
    // 调用获取数据
    this.detailText(e.detail)
  },
  /**
   * 调用详情描述跟进详细，通话等接口
   * author wl
   * tactics                  调用的方法
   * resourceCode      房客编号
   */
  detailText: throttle(function (tactics, code) {
    // 停止播放
    innerAudioContext?this.stopTelRecord():''
    wx.showNavigationBarLoading()
    // 调取数据的model
    this.detailData = {
      "page": this.data.page,
      "size": 5,
      "resourceCode": code ?code:this.data.code,
      "tactics": tactics
    }
    if (tactics === '3') {
      this.detailSmsList(0)
      return
    }
    // 调用相关方法数据接口
    request.queryInfo(this.detailData).then(res => {
      // 数据更新
      this.setData({ infoContent: res })
      wx.hideNavigationBarLoading()
    })
  }, 1000),
  /**
   * 切换房客详情内的短信号码
   * @author wl
   */
  checkoutIndex (e) {
    this.tabIndex = e.currentTarget.dataset.index
    let customerPhone = e.currentTarget.dataset.phone
    this.detailSmsList(this.tabIndex, customerPhone)
  },
  /**
   * 调用房客详情短信列表接口
   * @author wl
   */
  detailSmsList(index, customerPhone) {
    request.messageNum(this.data.code).then((res) => {
      this.setData({
        smsTabData: { tab: res, smsList: null },
        page: 1
      })
      this.smsdata = {
        customerPhone: customerPhone ?customerPhone:res[0].realNumber,
        page: this.data.page,
        pageSize: 5,
        resourceCode: this.data.code
      }
      this.getDetailSms(index)
    })
  },
  /**
   * 获取房客源详情短信列表
   * @author wl
   */
  getDetailSms(index, have) {
    this.tabIndex = index
    request.queryMessage(this.smsdata).then((res) => {
      have ? this.data.smsTabData.smsList.content = [...this.data.smsTabData.smsList.content, ...res.recordingModels.content]:this.data.smsTabData.smsList = res.recordingModels
      this.data.smsTabData.index = index
      this.setData({ smsTabData: this.data.smsTabData })
      wx.hideNavigationBarLoading()
      // 获取短信列表的高度
      this.domInter('.sms-height', this).then((css) => {
        if (css) {
          this.data.smsTabData.smsHeight = css.height
          this.setData({ smsTabData: this.data.smsTabData })
        }
      })
      wx.hideLoading()
    })
  },
  /**
   * 点击查看房客源详情tab更多
   * author wl
   */
  seeMore: function () {
    wx.showNavigationBarLoading()
    // 设置页数
    this.detailData.page = this.data.page += 1
    this.smsdata ? this.smsdata.page = this.data.page : ''
    // 调取对应的数据
    this.detailData.tactics !== "3"?request.queryInfo(this.detailData).then(res => {
      wx.hideNavigationBarLoading()
      // 文本数据
      if (res.contentList) {
        // 循环往渲染model中添加新的数据
        res.contentList.forEach((el) => {
          this.data.infoContent.contentList.push(el)
        })
      }
      // 通话数据
      if (res.callRecordsInfoMiniModelList) {
        // 循环往渲染model中添加新的数据
        res.callRecordsInfoMiniModelList.forEach((el) => {
          this.data.infoContent.callRecordsInfoMiniModelList.push(el)
        })
      }
      // 数据更新
      this.setData({ infoContent: this.data.infoContent })
      // wx.hideLoading()
    }) : this.getDetailSms(this.tabIndex, 'have')
  },
  /**
   * 房客详情截取图片最多显示七张
   * @author wl
   */
  sliceImage(mediaContents) {
    let lastArr = mediaContents
    lastArr.forEach((el, i) => {
      this.data.lastImagelist = [...this.data.lastImagelist, ...el.contents]
    })
    if (this.data.lastImagelist.length >= 7) {
      let imageArr = this.data.lastImagelist.splice(0, 7)
      this.setData({ lastImagelist: imageArr })
    } else {
      this.setData({ lastImagelist: this.data.lastImagelist })
    }
  },
  /**
  * 获取短信列表高度
  * author wl
  */
  domInter: function (value, _this) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        // 获取当前的查询
        var query = wx.createSelectorQuery().in(_this)
        // 查询当前dom的信息
        query.select(value).boundingClientRect(function (res) {
          resolve(res)
        }).exec()
      }, 200)
    })
  }
}